package com.example.test2.activity;


import android.content.res.ColorStateList;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ImageView;

import androidx.annotation.NonNull;

import com.bumptech.glide.Glide;
import com.example.test2.R;
import com.example.test2.databinding.ActivityClothesBinding;
import com.example.test2.db.entity.Clothes;
import com.example.test2.db.entity.ClothesInfo;
import com.example.test2.utils.ToastUtils;
import com.example.test2.utils.Tools;

import java.util.ArrayList;
import java.util.List;

//商品详情页
public class ClothesActivity extends BaseActivity<ActivityClothesBinding> implements View.OnClickListener {
    private String name;
    private boolean collection;
    private Clothes clothes;

    @Override
    protected void initData() {
        super.initData();
        Tools.setStatusBarColor(this, R.color.white);
        getBinding().titleBar.setLeftIcon(R.drawable.bt_back_white);
        getBinding().titleBar.setRightIcon(R.drawable.ic_cart);
        getBinding().titleBar.setBgColor(this,R.color.transparent);
        initContent();
    }

    private void initContent() {
        clothes = (Clothes) getIntent().getSerializableExtra("clothes");
        name = clothes.getName();
        getBinding().tvAddress.setText(clothes.getAddress());
        getBinding().tvName.setText(name+" " + clothes.getMsg());
        getBinding().tvView.setText("已售"+clothes.getView());
        String price = clothes.getPrice();
        if (price.contains("/")){
            getBinding().tvPrice.setText(price.substring(0, price.indexOf("/")));
        }else {
            getBinding().tvPrice.setText(price);
        }
        getBinding().tvShopName.setText(clothes.getShopName());
        getBinding().tvCost.setText(clothes.getCost());
        getBinding().tvSafeguard.setText(clothes.getSafeguard());
        getBinding().ratingBar.setSupportSecondaryProgressTintList(ColorStateList.valueOf(getResources().getColor(R.color.text_red)));
        getBinding().ratingBar.setSecondaryProgress(8);
        initBanner();
    }

    @Override
    protected void initClick() {
        super.initClick();
        getBinding().titleBar.setLeftIconOnClickListener(view -> {
            finish();
        });
        getBinding().llCollection.setOnClickListener(this);
        getBinding().tvBuy.setOnClickListener(this);
        getBinding().tvCart.setOnClickListener(this);
    }
    private void initBanner() {
        List<Integer> images = new ArrayList<>();
        List<String> images2 = new ArrayList<>();
        if (clothes.getUserAdd()){
            images2.add(clothes.getPath());
            images2.add(clothes.getPath());
            images2.add(clothes.getPath());
            getBinding().xbanner.setData(images2, null);
            getBinding().xbanner.loadImage((banner, model, view, position) ->
                    Glide.with(ClothesActivity.this).load(images2.get(position)).into((ImageView) view));
        }else {
            images.add(ClothesInfo.valueOf(name).getImage());
            images.add(ClothesInfo.valueOf(name).getImage());
            images.add(ClothesInfo.valueOf(name).getImage());
            getBinding().xbanner.setData(images, null);
            getBinding().xbanner.loadImage((banner, model, view, position) ->
                    Glide.with(ClothesActivity.this).load(images.get(position)).into((ImageView) view));
        }

    }
    @Override
    protected int initLayout() {
        return R.layout.activity_clothes;
    }

    @Override
    protected ActivityClothesBinding onCreateViewBinding(@NonNull LayoutInflater layoutInflater) {
        return ActivityClothesBinding.inflate(layoutInflater);
    }
    @Override
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.ll_collection:
                if (collection) {
                    collection = false;
                    getBinding().ivCollection.setImageResource(R.drawable.ic_collection);
                } else {
                    collection = true;
                    getBinding().ivCollection.setImageResource(R.drawable.ic_collection2);
                }
                break;
            case R.id.tv_buy:
                ToastUtils.getInstance().show(ClothesActivity.this, "购买成功！");
                break;
            case R.id.tv_cart:
                ToastUtils.getInstance().show(ClothesActivity.this, "加入购物车成功！");
                break;
        }
    }

}
